package com.jspgou.cms.action.store;

import static com.jspgou.cms.Constants.STORE_SYS;
import static com.jspgou.common.page.SimplePage.cpn;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Dictionary;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jspgou.cms.entity.Category;
import com.jspgou.cms.entity.Consult;
import com.jspgou.cms.entity.Discuss;
import com.jspgou.cms.entity.ShopMember;
import com.jspgou.cms.entity.Store;
import com.jspgou.cms.entity.StoreExt;
import com.jspgou.cms.entity.StoreModel;
import com.jspgou.cms.entity.StoreTxt;
import com.jspgou.cms.manager.CategoryMng;
import com.jspgou.cms.manager.ConsultMng;
import com.jspgou.cms.manager.DiscussMng;
import com.jspgou.cms.manager.OrderMng;
import com.jspgou.cms.manager.ProductMng;
import com.jspgou.cms.manager.StoreMng;
import com.jspgou.cms.manager.StoreModelMng;
import com.jspgou.cms.web.CmsUtils;
import com.jspgou.cms.web.ShopFrontHelper;
import com.jspgou.cms.web.SiteUtils;
import com.jspgou.common.page.Pagination;
import com.jspgou.common.web.CookieUtils;
import com.jspgou.common.web.springmvc.MessageResolver;
import com.jspgou.core.entity.ShopDictionary;
import com.jspgou.core.entity.Website;
import com.jspgou.core.manager.ShopDictionaryMng;

@Controller
public class StoreFroAct {
	private static final Logger log = LoggerFactory.getLogger(StoreFroAct.class);

	/**
	 * 店铺中心页
	 */
	public static final String STORE_CENTER = "tpl.storeCenter";
	/**
	 * 店铺资料修改页
	 */
	public static final String STORE_EDIT = "tpl.storeEdit";
	/**
	 * 店铺注册页
	 */
	public static final String STORE_REGISTRER = "tpl.storeRegister";
	/**
	 * 店铺评价列表页
	 */
	public static final String STORE_DISCUSS_LIST = "tpl.storeDisscussList";
	/**
	 * 店铺评价查看页
	 */
	public static final String STORE_DISCUSS_VIEW = "tpl.storeDisscussView";
	/**
	 * 店铺咨询列表页
	 */
	public static final String STORE_CONSULT_LIST = "tpl.storeConsultList";
	/**
	 * 店铺咨询列表页
	 */
	public static final String STORE_CONSULT_REPLAY = "tpl.storeConsultReplay";
	/**
	 * 店铺满就送设置页
	 */
	public static final String STORE_FULL_GIVE = "tpl.storeFullGive";

    //店铺中心首页	
	@RequestMapping(value = "/store/index.jspx", method = RequestMethod.GET)
	public String list(HttpServletRequest request,Integer pageNo, 
			HttpServletResponse response, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		model.addAttribute("member", member);
		Store store=member.getStore();
		//用户店铺中心
		if(store!=null){
			if(!store.getExamine()){
				return ShopFrontHelper.showMessage(request, model, "error.storeNotCheck");
			}
			Pagination pagination = productMng.getPage(web.getId(),null,store.getId(),
					null,null,null,false,null,null,null,
					null,null,null,null,cpn(pageNo), CookieUtils.getPageSize(request));
			Pagination paginationList = productMng.getPage(web.getId(),null,store.getId(),
					null,null,null,null,null,null,null,
					null,null,null,null,cpn(pageNo), CookieUtils.getPageSize(request));
            List<Object> o=orderMng.getStoreTotalOrder(store.getId());
            Long[] a=(Long[])o.get(0);
            BigDecimal[] b=(BigDecimal[]) o.get(1);
            model.addAttribute("a", a);
            model.addAttribute("b", b);
			model.addAttribute("pagination", pagination);
			model.addAttribute("paginationList", paginationList);
			model.addAttribute("store", store);
			ShopFrontHelper.setCommonData(request, model, web);
			ShopFrontHelper.frontPageData(request, model);
			return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,STORE_CENTER), request);
		}else{
			return "redirect:../registerStore.jspx";
		}
	}
	
	//前台修改店铺资料
	@RequestMapping(value = "/store/edit.jspx", method = RequestMethod.GET)
	public String edit(String cl,HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		Store store=member.getStore();
		if(!store.getExamine()){
			return ShopFrontHelper.showMessage(request, model, "error.storeNotCheck");
		}
		List<Category> categoryList=categoryMng.getList(web.getId());
		model.addAttribute("categoryList", categoryList);
		model.addAttribute("member", member);
		model.addAttribute("store", store);
		model.addAttribute("cl", cl);
		if(store.getModelpermission()){
			List<StoreModel> smlist=storeModelMng.getList();
			model.addAttribute("smlist", smlist);
		}else{
			StoreModel storeModel=storeModelMng.getByDefaultModel(true);
			if(storeModel==null){
				storeModel=storeModelMng.getList().get(0);
			}
			model.addAttribute("storeModel", storeModel);
		}
		ShopFrontHelper.setCommonData(request, model, web);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,
				STORE_EDIT), request);
	}
	
	//前台更新店铺资料
	@RequestMapping(value = "/store/update.jspx", method = RequestMethod.POST)
	public String update(Store bean, StoreExt ext,StoreTxt txt,Long categoryId,
			Long storeModelId,HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		List<Category> categoryList=categoryMng.getList(web.getId());
		model.addAttribute("categoryList", categoryList);
		storeModelId=(long)0;
		Store store=storeMng.updateFro(bean, ext, txt, categoryId,storeModelId);
		model.addAttribute("member", member);
		model.addAttribute("store", store);
		ShopFrontHelper.setCommonData(request, model, web);
		return "redirect:../store/edit.jspx";
	}
	
	//
	@RequestMapping(value = "/store/discussList*.jspx")
	public String discussList(String cl,HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		
		//*获取所有评价类型，传递到店铺评价列表页
		List<ShopDictionary> discussTypes=dictionaryMng.getList("discussType");
		//用户没有登录，跳转到登录页
		if(member == null){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		Store store=member.getStore();
		if(!store.getExamine()){
			return ShopFrontHelper.showMessage(request, model, "error.storeNotCheck");
	   }
		model.addAttribute("store", store);
		model.addAttribute("discussTypes", discussTypes);
		model.addAttribute("cl", cl);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,STORE_DISCUSS_LIST), request);
	}
	
	@RequestMapping(value = "/store/discussView*.jspx")
	public String discussView(Long id,Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		Store store=member.getStore();
		if(!store.getExamine()){
			return ShopFrontHelper.showMessage(request, model, "error.storeNotCheck");
	   }
		
		//* 获取所有评价类型，传递给前台
		List<ShopDictionary> discussTypes=dictionaryMng.getList("discussType");
		model.addAttribute("discussTypes", discussTypes);
		
		Discuss discuss=discussMng.findById(id);
		model.addAttribute("store", store);
		model.addAttribute("bean", discuss);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,STORE_DISCUSS_VIEW), request);
		
	}
	
	@RequestMapping(value="/store/discussReplay.jspx")
	public String discussReplay(Long discussId,String replay,Integer pageNo,
			HttpServletRequest request, ModelMap model){
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		
		
		List<ShopDictionary> discussTypes=dictionaryMng.getList("discussType");
		model.addAttribute("discussTypes", discussTypes);
		Discuss discuss =discussMng.findById(discussId);
		discuss.setReplay(replay);
		discussMng.save(discuss);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		Store store=member.getStore();
		model.addAttribute("store", store);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,STORE_DISCUSS_LIST), request);
	}
	
	@RequestMapping(value = "/store/consultList*.jspx")
	public String consultList(String cl,HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		Store store=member.getStore();
		if(!store.getExamine()){
			return ShopFrontHelper.showMessage(request, model, "error.storeNotCheck");
	    }
		
		//将字典数据传入前台
		List<ShopDictionary> consultTypes = dictionaryMng.getList("consultType");
		model.addAttribute("consultTypes", consultTypes);
		
		model.addAttribute("store", store);
		model.addAttribute("cl", cl);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,STORE_CONSULT_LIST), request);
	}
	
	@RequestMapping(value = "/store/consultReplay.jspx")
	public String consultReplay(Long id,Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		Store store=member.getStore();
		if(!store.getExamine()){
			return ShopFrontHelper.showMessage(request, model, "error.storeNotCheck");
	    }
		Consult consult=consultMng.findById(id);
		model.addAttribute("store", store);
		model.addAttribute("consult", consult);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,STORE_CONSULT_REPLAY), request);
	}
	
	@RequestMapping(value = "/store/consultReplaySave.jspx")
	public String consultReplaySave(Consult bean,Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		bean.setReplayTime(new Date());
		consultMng.update(bean);
		return "redirect:consultList_"+pageNo+".jspx";
	}
	
	@RequestMapping(value = "/store/fullGive.jspx")
	public String fullGive(HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		Store store=member.getStore();
		model.addAttribute("store", store);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(STORE_SYS, MessageResolver.getMessage(request,STORE_FULL_GIVE), request);
	}
	
	@RequestMapping(value = "/store/fullGiveUpdate.jspx")
	public String fullGiveUpdate(Long[] fullGiveIds,
			HttpServletRequest request, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		Store store=member.getStore();
		model.addAttribute("store", store);
		storeMng.update(store,fullGiveIds);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return "redirect:fullGive.jspx";
	}

	@Autowired
	private StoreMng storeMng;
	@Autowired
	private ConsultMng consultMng;
	@Autowired
	private DiscussMng discussMng;
	@Autowired
	private CategoryMng categoryMng;
	@Autowired
	private StoreModelMng storeModelMng;
	
	//店铺中心显示咨询类型，需要将字典传入前台页面
	@Autowired
	private ShopDictionaryMng dictionaryMng;
	@Autowired
	private ProductMng productMng;
	@Autowired
	private OrderMng orderMng;

}
